package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.admin.entity.AdChannel;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.BeanHelper;
import com.heima.model.admin.dtos.ChannelDto;
import org.apache.commons.lang3.StringUtils;
import org.omg.SendingContext.RunTime;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Queue;

/**
 * <p>
 * 频道信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-02-12
 */
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    /**
     * 分页查询频道数据
     * @param dto
     * @return
     */
    @Override
    public PageResult<ChannelDto> findByPage(ChannelDto dto) {
//        设置分页条件
        IPage<AdChannel> ipage = new Page<>(dto.getPage(),dto.getSize());
//        设置查询条件
        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
//        where name like %?%
        queryWrapper.like(StringUtils.isNotBlank(dto.getName()),AdChannel::getName,dto.getName());
//        分页查询
        IPage<AdChannel> adChannelIPage = page(ipage, queryWrapper);
        if(CollectionUtils.isEmpty(adChannelIPage.getRecords())){
//            结果为空
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

//        对象转换
        List<ChannelDto> channelDtos = BeanHelper.copyWithCollection(adChannelIPage.getRecords(), ChannelDto.class);

//        构造返回结果
        PageResult<ChannelDto> pageResult = new PageResult(dto.getPage(),
                dto.getSize(),
                adChannelIPage.getTotal(),
                channelDtos);
        return pageResult;
    }
    /**
     * 保存
     * @param dto
     * @return
     */
    @Override
    public void channelSave(ChannelDto dto) {
        if(StringUtils.isBlank(dto.getName())){
            throw new LeadException(AppHttpCodeEnum.CHANNELNAME_IS_NULL);
        }
//        查数据库，name必须唯一
//        select count(1) from ad_channel where name=?
        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdChannel::getName,dto.getName());
        int count = count(queryWrapper);
        if(count>0){
//            数据库中已经存在name
            throw new LeadException(AppHttpCodeEnum.DATA_EXIST);
        }
//        对象转换
        AdChannel adChannel = BeanHelper.copyProperties(dto, AdChannel.class);
        adChannel.setCreatedTime(new Date());
//        保存
        save(adChannel);
    }

    /**
     * 修改
     * @param dto
     * @return
     */
    @Override
    public void channelUpdate(ChannelDto dto) {
        if(dto.getId() == null || dto.getId()==0){
            throw new LeadException(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(StringUtils.isBlank(dto.getName())){
            throw new LeadException(AppHttpCodeEnum.CHANNELNAME_IS_NULL);
        }
//        select count(1) from ad_channel where name=? and id  <> #id
        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.
                eq(AdChannel::getName,dto.getName()).
                ne(AdChannel::getId,dto.getId());
        int count = count(queryWrapper);
        if(count>0){
            //            数据库中已经存在name
            throw new LeadException(AppHttpCodeEnum.DATA_EXIST);
        }
//        对象转换
        AdChannel adChannel = BeanHelper.copyProperties(dto, AdChannel.class);
//        根据主键id 更新
        updateById(adChannel);
    }
    /**
     * 删除
     * @param channelId
     * @return
     */
    @Override
    public void channelDelete(Integer channelId) {

//        根据id查询数据
        AdChannel adChannel = getById(channelId);
        if(adChannel.getStatus()){
//            频道有效
            throw new LeadException(AppHttpCodeEnum.DATA_CAN_NOT_DEL);
        }

        removeById(channelId);

    }
}
